<template>
  <div class="about">
    <div class="container">
      <div style="margin: 10px 0">
        <el-input placeholder="请输入操作名称" v-model="operation" style="width: 200px;margin-right: 20px"></el-input>
        <el-input placeholder="请输入操作类型" v-model="type" style="width: 200px;margin-right: 20px"></el-input>
        <el-button type="primary" @click="load(pageNum)">查询</el-button>
        <el-button type="warning" @click="returnInfo">重置</el-button>
      </div>

      <el-table :data="tableData" @selection-change="handleSelectionChange" style="border-radius: 10px"
                :header-cell-style="{background:'aliceblue',height:'45px', color:'#666'}">
        <el-table-column align="center" prop="id" label="序号"></el-table-column>
        <el-table-column align="center" prop="user" label="操作人"></el-table-column>
        <el-table-column align="center" prop="operation" label="操作名称"></el-table-column>
        <el-table-column prop="type" label="操作类型">
          <template v-slot="scope">
            <el-tag type="success" v-if="scope.row.type ==='新增'">{{ scope.row.type }}</el-tag>
            <el-tag type="primary" v-if="scope.row.type ==='修改'">{{ scope.row.type }}</el-tag>
            <el-tag type="danger" v-if="scope.row.type ==='删除'">{{ scope.row.type }}</el-tag>
            <el-tag type="danger" v-if="scope.row.type ==='批量删除'">{{ scope.row.type }}</el-tag>
            <el-tag type="success" v-if="scope.row.type ==='登录'">{{ scope.row.type }}</el-tag>
            <el-tag type="success"v-if="scope.row.type ==='注册'">{{ scope.row.type }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="time" label="发布时间"></el-table-column>
        <el-table-column align="center" prop="ip" label="ip"></el-table-column>
        <el-table-column align="center" width="170px" label="操作">
          <template v-slot="scope" style="width: 340px">
            <el-button style="width: 65px;" type="danger" plain @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin: 20px 0">
        <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>

      <el-dialog title="添加新闻" :visible.sync="FormAddInfo" width="40%">
        <el-form label-width="80px" style="padding-right: 30px" ref="formRef" :rules="rules" :model="users"
                 class="demo-form-inline">
          <el-form-item prop="title" style="margin-top: 30px;" label="标题">
            <el-input v-model="users.title"></el-input>
          </el-form-item>

          <el-form-item prop="content" style=" margin-left: 2px" label="内容">
            <el-input type="textarea" v-model="users.content"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button @click="FormAddInfo = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>

      </el-dialog>

      <el-dialog title="修改新闻" :visible.sync="FormUpdateInfo" width="40%" @close="onClose01">
        <el-form label-width="80px" style="padding-right: 30px" ref="formRef" :rules="rules" :model="forms"
                 class="demo-form-inline">

          <el-form-item prop="title" style="margin-top: 30px;" label="标题">
            <el-input v-model="forms.title"></el-input>
          </el-form-item>

          <el-form-item prop="content" style=" margin-left: 2px" label="内容">
            <el-input type="textarea" v-model="forms.content"></el-input>
          </el-form-item>

        </el-form>

        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button @click="FormUpdateInfo = false">取 消</el-button>
          <el-button type="primary" @click="onUpdate">确 定</el-button>
        </div>

      </el-dialog>

      <el-dialog title="新闻详情" :visible.sync="NewsInfo" width="40%" >
        <div class="w-e-text">
          <div v-html="content"></div>
        </div>

        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button type="primary" plain @click="NewsInfo = false">确 定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import request from "@/utils/request"

export default {
  data() {
    return {
      Info: JSON.parse(localStorage.getItem('user11') || '{}'),
      pageSize: 10,
      pageNum: 1,
      tableData: [],
      title: '',
      type: '',
      operation: '',
      total: 0,
      FormAddInfo: false,
      NewsInfo: false,
      user: JSON.parse(localStorage.getItem('user11') || '{}'),
      users: {},
      ids: [],
      forms: {},
      FormUpdateInfo: false,

      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'},
          {min: 2, max: 1000, message: '长度在 2 到 1000 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.load()
  },
  methods: {

    showContent(content) {
      this.NewsInfo = true
      this.content = content
    },

    //删除单条数据
    handleDelete(id) {
      this.$confirm('确定删除吗？', '提示', {type: 'warning'}).then(() => {
        request.delete('/logs/delete/' + id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.load(1)
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消删除'});
      })
    },

    returnInfo() {
      this.type = '',
          this.operation = '',
          this.load()
    },
    load(pageNum) {
      this.pageNum = pageNum || 1
      request.get('/logs/selectByPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          operation: this.operation,
          type: this.type,
        }
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.el-tooltip__popper{
  max-width:200px !important;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
  border-radius: 50%;
  margin-top: -10px;
  margin-bottom: 10px;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
